﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace DIS.Infrastructure.Entities.Repository
{
    [Table("T_Project")]
    public class T_Project : IEntity<T_Project>
    {
        [Key]
        [Column(TypeName = "int")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(50)]
        public string ProjectCode { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(100)]
        public string ProjectName { get; set; }

        
        public int IsDeleted { get; set; }


        public override T_Project Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Project Find(System.Linq.Expressions.Expression<Func<T_Project, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Project.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.ProjectCode = item.ProjectCode;
                    this.ProjectName = item.ProjectName;
                    this.IsDeleted = item.IsDeleted;

                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Project, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Project.Any(where);
            }
        }

        public override T_Project FindLast(System.Linq.Expressions.Expression<Func<T_Project, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Project.OrderByDescending(c => c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.ProjectCode = item.ProjectCode;
                    this.ProjectName = item.ProjectName;
                    this.IsDeleted = item.IsDeleted;
                    return this;
                }
                return null;
            }
        }
    }
}
